

// import { areaList } from './../../miniprogram_npm/@vant/area-data/index';
// import Notify from './../../miniprogram_npm/vant-weapp/notify/notify';
const util = require('../../utils/util.js');
const chooseLocation = requirePlugin('chooseLocation');
Page({
  data: {
    addressType: '',//0：服务地址，1：收获地址
    addrId: '',
    username: '',
    mobile: '',

    province: '',
    city: '',
    region: '',
    latitude: '',//纬度
    longitude: '',//经度
    addrLine1: '',
    addrLine2: '',
    // 
    swiChecked: false,
    //  
    // selectIndex: 0,
    // list: [
    //   {
    //     name: '80平米以内',
    //     id: 0
    //   },
    //   {
    //     name: '80平米',
    //     id: 1
    //   },
    //   {
    //     name: '80-99平米',
    //     id: 2
    //   },
    //   {
    //     name: '100-110平米',
    //     id: 3
    //   },
    //   {
    //     name: '120-139平米',
    //     id: 4
    //   },
    //   {
    //     name: '140-159平米',
    //     id: 5
    //   },
    //   {
    //     name: '160平米以上',
    //     id: 6
    //   },
    // ]
  },
  swOnChange({ detail }) {
    this.setData({ swiChecked: detail });
  },
  handleSubmit() {
    let sData = this.data;
    console.log(sData)
    if (sData.username.trim() == '') {
      wx.showModal({
        title: '温馨提示',
        content: '姓名不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (!/^1[3456789]\d{9}$/.test(sData.mobile)) {
      wx.showModal({
        title: '温馨提示',
        content: '电话号码不正确！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    if (sData.addrLine1 == '') {
      wx.showModal({
        title: '温馨提示',
        content: '地址不能为空！',
        confirmColor: '#F08302',
        showCancel: false,
        success(res) {
        }
      })
      return
    }
    let data = {
      "name": sData.username,
      "phone": sData.mobile,
      "province": sData.province,
      "city": sData.city,
      "region": sData.region,
      "addrLine1": sData.addrLine1,
      "addrLine2": sData.addrLine2,
      "addrTags": "",
      "defaultFlg": sData.swiChecked ? 0 : 1,
      "type": sData.addressType,
      "latitude": sData.latitude,
      "longitude": sData.longitude,

    }
    console.log(data)
    // return
    if (sData.addrId) {
      data.id = sData.addrId;
      util.https('addrMgmt/addrMod', data, (res) => {
        console.log(res)
        wx.showModal({
          title: '温馨提示',
          content: '提交成功！',
          confirmColor: '#F08302',
          showCancel: false,
          success(res) {
            wx.navigateBack({
              delta: 1
            })
          }
        })

      }, (res) => {
        console.log(res)
        wx.showModal({
          title: '温馨提示',
          content: res.errorMsg,
          confirmColor: '#F08302',
          showCancel: false,
          success(res) {
          }
        })

      })
    } else {
      util.https('addrMgmt/addrAdd', data, (res) => {
        console.log(res)
        wx.showModal({
          title: '温馨提示',
          content: '提交成功！',
          confirmColor: '#F08302',
          showCancel: false,
          success(res) {
            wx.navigateBack({
              delta: 1
            })
          }
        })
      }, (res) => {
        console.log(res)
        wx.showModal({
          title: '温馨提示',
          content: res.errorMsg,
          confirmColor: '#F08302',
          showCancel: false,
          success(res) {

          }
        })
      })
    }
  },
  clickItem(e) {
    const data = e.currentTarget.dataset;
    this.setData({
      selectIndex: data.index
    })
  },
  checkAddr() {
    const key = 'GIYBZ-IOBWU-JJAVH-GOKCS-VZA56-KCBCO'; //使用在腾讯位置服务申请的key
    const referer = '欣佳源'; //调用插件的app的名称
    const category = '';
    wx.setStorageSync('addressEditMap', 1);
    wx.navigateTo({
      url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&category=${category}`,
    });
  },
  // cancelAc() {
  //   this.setData({
  //     showAc: false
  //   })
  // },
  // confirmAc(ss) {
  //   console.log(ss)
  //   let item = ss.detail.values;
  //   this.setData({
  //     province: item[0].name,
  //     city: item[1].name,
  //     region: item[2].name,
  //     address: item[0].name + "/" + item[1].name + "/" + item[2].name,
  //     showAc: false
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let addressType = wx.getStorageSync('addressType');
    this.setData({
      addressType: addressType
    });

    let fromType = wx.getStorageSync('addressEditMap');
    if (fromType === 1) {
      wx.setStorageSync('addressEditMap', '');
      const location = chooseLocation.getLocation();
      console.log(location)
      if (location) {
        this.setData({
          province: location.province,
          city: location.city,
          region: location.district,
          latitude: location.latitude,
          longitude: location.longitude,
          addrLine1: location.address,
        })
        console.log(this.data)
      }
    } else {
      wx.getStorage({
        key: 'addressedit',
        success: (res) => {
          if (res.data) {
            let item = res.data;
            this.setData({
              username: item.name,
              mobile: item.phone,
              province: item.province,
              city: item.city,
              region: item.region,
              addrLine1: item.addrLine1,
              addrLine2: item.addrLine2,
              latitude: item.latitude,
              longitude: item.longitude,
              addrId: item.id,
              swiChecked: item.defaultFlg === 0 ? true : false
            })
          } else {

          }
        },
        fail: (res) => {
          console.log(res)
        }
      })
    }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
  
})